package com.doform.android.xianggucheng.activity.login;

import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.doform.android.xianggucheng.R;
import com.doform.android.xianggucheng.activity.BaseTitleActivity;
import com.doform.android.xianggucheng.bean.RegisterBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqRegister2;
import com.doform.android.xianggucheng.bean.request_bean.ReqRetrieve;
import com.doform.android.xianggucheng.bean.request_bean.ReqUpdatePassword;
import com.doform.android.xianggucheng.retrofit.HttpManager;
import com.doform.android.xianggucheng.retrofit.subscriber.ErrorHandleSubscriber;
import com.doform.android.xianggucheng.utils.Constans;
import com.doform.android.xianggucheng.utils.RexUtils;
import com.doform.android.xianggucheng.utils.TsUtils;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者 Y_MS
 * Created by ${APEN} on 2017-08-18.
 * GitHub：https://github.com/cxydxpx
 */

public class RetrievePassWordActivity extends BaseTitleActivity {

    private TimeCount time;
    private String phone;

    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            //参数依次为总时长,和计时的时间间隔
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            //计时完毕时触发
            btn_send.setText("重新获取");
            btn_send.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            //计时过程显示
            btn_send.setClickable(false);
            btn_send.setText(millisUntilFinished / 1000 + "秒");
        }
    }

    @BindView(R.id.btn_send)
    Button btn_send;

    @BindView(R.id.et_phone)
    EditText etPhone;

    @BindView(R.id.et_code)
    EditText etCode;

    @BindView(R.id.et_password)
    EditText etPassword;

    @Override
    protected int layoutResId() {
        return R.layout.activity_retrieve;
    }

    @Override
    protected void init() {
        super.init();
        time = new TimeCount(60000, 1000);

        this.phone = getIntent().getStringExtra(Constans.EXTRA_RETRIEVE);

    }

    @Override
    protected void showView() {
        etPhone.setText(phone);
        titleBar.setTextTitle("找回密码");
    }

    private void beforeSend() {

        String mPhone = etPhone.getText().toString().trim();

        if (TextUtils.isEmpty(mPhone)) {
            TsUtils.showToast("请输入手机号");
            return;
        } else if (RexUtils.isMobileNO(mPhone)) {
            TsUtils.showToast("手机号格式不正确");
            return;
        }

        checkoutPhone(mPhone);
    }

    private void checkoutPhone(String mPhone) {
        HttpManager.instance()
                .getErCode2(new ReqRetrieve(mPhone))
                .subscribe(new ErrorHandleSubscriber<String>() {
                               @Override
                               public void onNext(@NonNull String s) {
                                   time.start();
                               }
                           }
                );
    }

    private void beforeNext() {
        String mPhone = etPhone.getText().toString().trim();
        String mCode = etCode.getText().toString().trim();
        String mPassword = etPassword.getText().toString().trim();

        if (TextUtils.isEmpty(mPhone)) {
            TsUtils.showToast("请输入手机号");
            return;
        } else if (RexUtils.isMobileNO(mPhone)) {
            TsUtils.showToast("手机号格式不正确");
            return;
        } else if (TextUtils.isEmpty(mCode)) {
            TsUtils.showToast("请输入验证码");
            return;
        } else if (TextUtils.isEmpty(mPassword)) {
            TsUtils.showToast("请输入密码");
            return;
        }

        updatePassword1(mPhone, mCode, mPassword);
    }

    //                .postCodeToService2(new ReqRegister2(mPhone, code))
//                .subscribe(new ErrorHandleSubscriber<Integer>() {
    private void updatePassword1(final String mPhone, String mCode, final String mPassword) {
        HttpManager
                .instance()
                .postCodeToService2(new ReqRegister2(mPhone, mCode))
                .subscribeOn(Schedulers.io())
                .flatMap(new Function<Integer, ObservableSource<RegisterBean>>() {
                             @Override
                             public ObservableSource<RegisterBean> apply(@NonNull Integer integer) throws Exception {
                                 return updatePassword2(mPhone, mPassword);
                             }
                         }
                ).subscribe(new ErrorHandleSubscriber<RegisterBean>() {
                                @Override
                                public void onNext(@NonNull RegisterBean s) {

                                    Intent intent = new Intent();
                                    intent.putExtra(Constans.EXTRA_LOGIN, s.getUsername());
                                    setResult(Constans.REQUEST_RETRIEVE, intent);
                                    finish();
                                    TsUtils.showToast("修改密码成功");
                                }
                            }
        );
    }

    private Observable<RegisterBean> updatePassword2(String mPhone, String mPassword) {
        return HttpManager
                .instance()
                .updatePassword(new ReqUpdatePassword(mPhone, mPassword));
    }

    @OnClick({R.id.btn_send, R.id.btn_next})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.btn_send:
                beforeSend();
                break;
            case R.id.btn_next:
                beforeNext();
                break;
            default:
                break;
        }
    }


}
